package com.dj.persistence.internal;

import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.config.TargetServer;
import org.eclipse.persistence.config.TargetDatabase;
import org.eclipse.persistence.config.ProfilerType;
import org.eclipse.persistence.tools.profiler.QueryMonitor;

import javax.persistence.spi.PersistenceUnitTransactionType;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.Map;
import java.util.HashMap;
import java.util.logging.Level;

import com.dj.persistence.internal.AccountAppSessionCustomizer;

/**
 * Created by IntelliJ IDEA.
 * User: Jacob
 * Date: Jul 19, 2008
 * Time: 12:52:21 PM
 * To change this template use File | Settings | File Templates.
 */
public class EntityManagerFactoryProvider {

    private static final String PERSISTENCE_UNIT_NAME = "djPU";

    private static EntityManagerFactory emf = null;

    private static Map createPUPropertiesMap() {
        Map propertiesMap = new HashMap();
        propertiesMap.put(PersistenceUnitProperties.TRANSACTION_TYPE, PersistenceUnitTransactionType.RESOURCE_LOCAL.name());
        propertiesMap.put(PersistenceUnitProperties.JDBC_DRIVER, "org.postgresql.Driver");
        propertiesMap.put(PersistenceUnitProperties.JDBC_URL, "jdbc:postgresql://localhost:5432/dj_db");
        propertiesMap.put(PersistenceUnitProperties.JDBC_USER, "jacob");
        propertiesMap.put(PersistenceUnitProperties.JDBC_PASSWORD, "kingcobra");
        propertiesMap.put(PersistenceUnitProperties.JDBC_READ_CONNECTIONS_MIN, "1");
        propertiesMap.put(PersistenceUnitProperties.JDBC_WRITE_CONNECTIONS_MIN, "1");
        // Configure logging. FINE ensures all SQL is shown
        propertiesMap.put(PersistenceUnitProperties.LOGGING_LEVEL, Level.ALL.toString());
        // Ensure that no server-platform is configured
        propertiesMap.put(PersistenceUnitProperties.TARGET_SERVER, TargetServer.None);
        propertiesMap.put(PersistenceUnitProperties.TARGET_DATABASE, TargetDatabase.PostgreSQL);

        propertiesMap.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.DROP_AND_CREATE);
        propertiesMap.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DEFAULT_DDL_GENERATION_MODE);
        propertiesMap.put(PersistenceUnitProperties.PROFILER, ProfilerType.QueryMonitor);
        //propertiesMap.put(PersistenceUnitProperties.SESSION_CUSTOMIZER, AccountAppSessionCustomizer.class.getName());
        propertiesMap.put(PersistenceUnitProperties.SESSION_EVENT_LISTENER_CLASS, AccountAppSessionEventListener.class.getName());
        
        return propertiesMap;
    }

    public static EntityManagerFactory getEntityManagerFactoryIntance() {
        if (null == emf) {
            emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, createPUPropertiesMap());
        }
        return emf;
    }

}
